package com.cloud.school.bus.teacherhelper.modules.notice.adapter;

import java.util.List;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.ImageUtil;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.TeacherHelperApplication;
import com.cloud.school.bus.teacherhelper.entitys.Student;
import com.cloud.school.bus.teacherhelper.entitys.UploadFile;
import com.cloud.school.bus.teacherhelper.modules.stuclass.StudentDetailActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 学生选择adapter
 * 
 * @author lenovo
 * 
 */
public class StuSelectAdapter extends BaseAdapter {

	private static final String ALL_SELECT = "all_select";
	private static final String ITEM = "item";

	private Context mContext;
	private TeacherHelperApplication mApplication;
	private List<Student> mStudents;
	/** 是否是全部选择 */
	private boolean mAllSelected = false;
	/** 上传文件的时候用到其他时候为null */
	private List<UploadFile> mUploadFiles;
	/** 上传文件界面用到 */
	private ViewPager mViewPager;

	public StuSelectAdapter(Context context, List<Student> students) {
		mContext = context;
		mStudents = students;
		mApplication = (TeacherHelperApplication) mContext
				.getApplicationContext();
	}

	public StuSelectAdapter(Context context, List<Student> students,
			List<UploadFile> uploadFiles, ViewPager viewPager) {
		this(context, students);
		mUploadFiles = uploadFiles;
		mViewPager = viewPager;
	}

	@Override
	public int getCount() {
		return mStudents.size() + 1;
	}

	@Override
	public Object getItem(int arg0) {
		if (0 == arg0) {
			return "";
		}
		return mStudents.get(arg0 - 1);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {

		if (null == arg1) {
			if (0 == arg0) {
				arg1 = LayoutInflater.from(mContext).inflate(
						R.layout.adapter_stu_select_all, null);
				arg1.setTag(ALL_SELECT);
			} else if (arg0 > 0) {
				arg1 = LayoutInflater.from(mContext).inflate(
						R.layout.adapter_stu_select_item, null);
				arg1.setTag(ITEM);
			}
		} else {
			if (0 == arg0 && !ALL_SELECT.equals(arg1.getTag())) {
				arg1 = LayoutInflater.from(mContext).inflate(
						R.layout.adapter_stu_select_all, null);
				arg1.setTag(ALL_SELECT);
			} else if (arg0 > 0 && !ITEM.equals(arg1.getTag())) {
				arg1 = LayoutInflater.from(mContext).inflate(
						R.layout.adapter_stu_select_item, null);
				arg1.setTag(ITEM);
			}
		}

		if (0 == arg0) {
			ImageView allSelectImageView = (ImageView) arg1
					.findViewById(R.id.allSelectImageView);
			ViewGroup mainLayout = (ViewGroup) arg1
					.findViewById(R.id.mainLayout);
			mainLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if (null != mViewPager) {
						if(mUploadFiles.get(mViewPager.getCurrentItem()).isAllSelected){
							if (null != mViewPager) {
								mUploadFiles.get(mViewPager.getCurrentItem()).studentIdList
										.clear();
							}
							clearAllSelected();
						}else {
							if (null != mViewPager) {
								for (int i = 0; i < mStudents.size(); i++) {
									mUploadFiles.get(mViewPager.getCurrentItem()).studentIdList
											.add(mStudents.get(i).studentid);
								}
							}
							allSelected();
						}
					} else if (mAllSelected) {
						clearAllSelected();
					} else {
						allSelected();
					}
				}
			});
		} else if (arg0 > 0) {
			final Student student = mStudents.get(arg0 - 1);
			final ImageView selectImageView = (ImageView) arg1
					.findViewById(R.id.selectImageView);
			if (student.isSelected) {
				selectImageView.setVisibility(View.VISIBLE);
			} else {
				selectImageView.setVisibility(View.GONE);
			}
			ImageView headImageView = (ImageView) arg1
					.findViewById(R.id.headImageView);
			if (!TextUtils.isEmpty(student.avatar)) {
				ImageLoader.getInstance().displayImage(student.avatar,
						headImageView,
						mApplication.mStudentCacheDisplayImageOptions,
						new SimpleImageLoadingListener() {
							@Override
							public void onLoadingComplete(String imageUri,
									View view, Bitmap loadedImage) {
								ImageView imageView = (ImageView) view;
//								imageView.setImageBitmap(loadedImage);
								if(imageUri.startsWith("http://")){
									imageView.setImageBitmap(loadedImage);
								}else if(imageUri.startsWith("file:///")){
									ImageUtil.setRotaingImageBitmap(imageUri.replace("file:///", "/"), loadedImage, imageView);
								}else {
									ImageUtil.setRotaingImageBitmap(imageUri, loadedImage, imageView);
								}
							}
						});
			}
			TextView stuNameTextView = (TextView) arg1
					.findViewById(R.id.stuNameTextView);
			stuNameTextView.setText(student.enname);

			ViewGroup itemLayout = (ViewGroup) arg1
					.findViewById(R.id.itemLayout);
			ViewGroup mainLayout = (ViewGroup) arg1
					.findViewById(R.id.mainLayout);
			mainLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					mAllSelected = false;
					if (null != mViewPager) {
						mUploadFiles.get(mViewPager.getCurrentItem()).isAllSelected = false;
					}
					student.isSelected = !student.isSelected;
					if (student.isSelected) {
						if (null != mViewPager) {
							mUploadFiles.get(mViewPager.getCurrentItem()).studentIdList
									.add(student.studentid);
						}
						selectImageView.setVisibility(View.VISIBLE);
					} else {
						if (null != mViewPager) {
							mUploadFiles.get(mViewPager.getCurrentItem()).studentIdList
									.remove(student.studentid);
						}
						selectImageView.setVisibility(View.GONE);
					}
				}
			});
		}

		return arg1;
	}

	private void allSelected() {
		selectedAllStudent();
		mAllSelected = true;
		if (null != mViewPager) {
			mUploadFiles.get(mViewPager.getCurrentItem()).isAllSelected = true;
		}
		notifyDataSetChanged();
	}

	/**
	 * 外部调用
	 */
	public void selectedAllStudent() {
		for (int i = 0; i < mStudents.size(); i++) {
			mStudents.get(i).isSelected = true;
		}
	}

	private void clearAllSelected() {
		mAllSelected = false;
		if (null != mViewPager) {
			mUploadFiles.get(mViewPager.getCurrentItem()).isAllSelected = false;
		}
		clearSelected();
		notifyDataSetChanged();
	}

	/**
	 * 外部调用
	 */
	public void clearSelected() {
		for (int i = 0; i < mStudents.size(); i++) {
			mStudents.get(i).isSelected = false;
		}
	}

}
